$(function() {
	init();
});
var oPage;
var _uri = 'salesAnalysis_report_week';
function init(){ 
       getHandler('common', {fun:'limit',code:'15'}, function(val){
			$('#buttonBox').html(val);
	},goBack);
	postHandler(_uri, {fun:'list'}, function(val){
		$('.fancyTable').html(val);
		postHandler(_uri, {fun:'titles'}, function(val){
			initTable();
			initPrintPopup(val);
			initPopupWindow();
			setTimeout('initFixedHeaderTable()',500);
                       
			initDateRange();
		},goBack);
	},goBack);
}

var _begin_time, _date_type;
function initDateRange(){
	postHandler(_uri, {fun:'date_range'}, function(val){
		var tok = val.split('_###_');
		if(tok.length==4){
			_date_type = tok[0];
			_begin_time = tok[2];
			$(document).attr('title', '销售分析报表(本'+tok[1]+')-不含销退');
			$('h1').eq(0).html($(document).attr('title')+'&nbsp;<span>('+tok[2]+' ~ '+tok[3]+')</span>');
			var radios = $('input[name=compare_time]:radio');
			radios.eq(0)[0].value = radios.eq(0)[0].nextSibling.nodeValue = '对比上'+tok[1];
			radios.eq(1)[0].value = radios.eq(1)[0].nextSibling.nodeValue = ' 对比去年本'+tok[1];
			if(tok[0]=='year') {
				radios.eq(0).parent().hide();
				radios.eq(1).prop('checked','checked');
			}else
				radios.eq(0).prop('checked','checked');
		}
	});
}
function compareTime(){
	var compare_time = getRadioVal('compare_time');
	var date = new Date(_begin_time);
	var prevDate = new Date(_begin_time);
	if(_date_type=='week')
		prevDate.setDate(prevDate.getDate()-7);
	else if(_date_type=='month')
		prevDate.setMonth(prevDate.getMonth()-1);
	else if(_date_type=='year')
		prevDate.setDate(prevDate.getDate()-365);
	var range = getDateRange(date, _date_type, '-');
	var prevRange = getDateRange(prevDate, _date_type, '-');
	postHandler(_uri, {fun:'query2',date_range:range, prev_date_range:prevRange, compare_time:compare_time}, function(val){
		location.href = 'salesAnalysis_compared_with_last_week.html';
		return false;
	});
	return false;
}
function goBack(){
	//location.href = 'salesAnalysis_report_week_search.html';
	return false;
}
function initTable(){
	var oChks = $('.checkbox_box_w').find('input:checkbox');
	var oTable = $('#myTable05');
	var _thead = oTable.find('thead');
	var _tbody = oTable.find('tbody');
	var oTh = _thead.find('tr:eq(0)').find('th');
	var oTr = _tbody.find('tr');
	oTh.each(function(idx){
		$(this).addClass('t_'+idx);
	});
	oTr.each(function(idx1){
		$(this).find('td').each(function(idx2){
			$(this).addClass('t_'+idx2);
		});
	});
}
function chkboxOk(){
	var oChks = $('.checkbox_box_w').find('input:checkbox');
	oPage = $('#myTable05').clone();
	var _thead = oPage.find('thead');
	var _tbody = oPage.find('tbody');
	var oTh = _thead.find('tr:eq(0)').find('th:gt(0)');
	var oTr = _tbody.find('tr');
	//alert(oTh1.eq(0).attr('colspan'));
	oChks.each(function(idx){
		var chk = $(this)[0].checked;
		var tok = $(this)[0].id.split('_');
		if(tok.length==2 && chk==false){
			oPage.find('.t_'+tok[1]).each(function(){$(this).remove();});
		}
	});
	openPrintPage();
	return false;
}
function openPrintPage(){
	var html = oPage.html();
	var title = $('title').html();
	var h1 = $('h1').html();
	var _html = '<html><body><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'+
		'<title>'+title+'</title><script type="text/javascript" src="layout/head.js"></script>'+
		'<style type="text/css" media="print">.no_print{display: none;}</style></head>'+
		'<body><table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><p class="small">'+
		h1+'</p></td><td style="text-align: right;">'+
		'<input class="no_print button_" type="button" value="打印" onclick="print();window.close();"/><input class="no_print button_" type="button" value="返回" onclick="window.close()"/></td>'+
		'</tr></table><div class=""><table class="fancyTable" id="myTable05" border="0" cellspacing="0" cellpadding="0">'+
		html+'</table></div></body></html>';
	var win = window.open('','newwindow', 'toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=n o, status=no');
	win.document.write(_html);
	win.document.close();
	oPage.remove();
}
function initPrintPopup(val){
	var oWin = $('.checkbox_box_w');
	var html = '<div><label><input name="checkbox" type="checkbox" id="" checked="checked"><strong style="color:#00aacc;">全部</strong></label></div><div class="checkbox_box"></div>'+
		'<input class="cancel button_" onclick="chkboxOk()" type="button" value="确定" />';
	oWin.html(html);
	var div = oWin.find('.checkbox_box');
	var tok = val.split('_####_');
	var idx = 0;
	for(var i=1;i<tok.length;i++){
		var tok2 = tok[i].split('_#_#_');
		var t1 = tok2[0];
		var tok3 = tok2[tok2.length-1].split('_###_');
		var _div = $('<div/>');
		var _html = '<label><input type="checkbox" name="checkbox" id="t_'+i+'" checked="checked"><strong>'+t1+'</strong></label>';
		if(tok2.length>1){
			_html += '<div class="checkbox_box" style="border-left-style: dotted;">';
			for(var j=0;j<tok3.length;j++){
				var t2 = tok3[j];
				_html += '<div><label><input type="checkbox" name="checkbox" id="t_'+i+'_'+j+'_'+idx+'" checked="checked">'+t2+'</label></div>';
				idx++;
			}
			_html += '</div>';
		}
		_div.html(_html).appendTo(div);
	}
	setTimeout('initChkbox()',500);
}
function initFixedHeaderTable(){
	$('.fancyTable').fixedHeaderTable({width:'1000px',  altClass: 'odd', footer: false, fixedColumns: 1});
        var ths=$('#myTable05 thead tr:eq(0) th');
        var len=ths.length;
        for(var i=0;i<len;i++){
            var w=$(ths[i]).width()+1;
            $('.fht-fixed-body .fht-thead tr th').eq(i).width(w);
            $('.fht-fixed-body .fht-thead tr th div').eq(i).width(w);
        }
}

function download(){
	location.href = _uri+'?fun=xls';
	return false;
}